<?php

/**
 * 夏日葵电商系统
 * http://www.xiarikui.com
 */

namespace Admin\Model;

use Think\Model\RelationModel;

class OrderModel extends RelationModel {

    protected $patchValidate = true; // 系统支持数据的批量验证功能，
    protected $_validate = array(
        array('consignee', 'require', '收货人称必须填写！', 1, '', 3), // 1 必须验证
        array('address', 'require', '地址必须填写', 1, '', 3), // 在新增的时候验证name字段是否唯一
    );

    /**
     * @param array $condition  搜索条件
     * @param string $order   排序方式
     * @param int $start    limit开始行
     * @param int $page_size  获取数量
     */
    public function getOrderList($condition, $order = '', $start = 0, $page_size = 20) {
        $res = M('order')->where($condition)->limit("$start,$page_size")->order($order)->select();
        return $res;
    }

    /**
     * 获取订单商品详情
     */
    public function getOrderGoods($order_id) {
        $sql = "SELECT g.*,o.*,(o.goods_num * o.member_goods_price) AS goods_total FROM __PREFIX__order_goods o " .
                "LEFT JOIN __PREFIX__goods g ON o.goods_id = g.goods_id WHERE o.order_id = $order_id";
        $res = $this->query($sql);
        return $res;
    }

    /**
     * 获取订单信息
     */
    public function getOrderInfo($order_id) {
        //  订单总金额查询语句		
        $order = M('order')->where("order_id = $order_id")->find();
        $order['address2'] = $this->getAddressName($order['province'], $order['city'], $order['district']);
        $order['address2'] = $order['address2'] . $order['address'];
        return $order;
    }

    /**
     * 根据商品型号获取商品
     */
    public function getSpecGoods($goods_id_arr) {
        if (!is_array($goods_id_arr))
            return false;
        foreach ($goods_id_arr as $key => $val) {
            $arr = array();
            $goods = M('goods')->where("goods_id = $key")->find();
            $arr['goods_id'] = $key; // 商品id
            $arr['goods_name'] = $goods['goods_name'];
            $arr['goods_sn'] = $goods['goods_sn'];
            $arr['market_price'] = $goods['market_price'];
            $arr['goods_price'] = $goods['shop_price'];
            $arr['cost_price'] = $goods['cost_price'];
            $arr['member_goods_price'] = $goods['shop_price'];
            $arr['goods_stock'] = $goods['store_count'];
            foreach ($val as $k => $v) {
                $arr['goods_num'] = $v['goods_num']; // 购买数量
                // 如果这商品有规格
                if ($k != 'key') {
                    $arr['spec_key'] = $k;
                    $spec_goods = M('goods_stock')->where("goods_id = $key and `key` = '{$k}'")->find();
                    $arr['spec_key_name'] = $spec_goods['key_name'];
                    $arr['member_goods_price'] = $arr['goods_price'] = $spec_goods['price'];
                    $arr['sku'] = $spec_goods['sku'];
                    $arr['goods_stock'] = $spec_goods['store_count'];
                }
                $order_goods[] = $arr;
            }
        }
        return $order_goods;
    }

    /**
     * 订单操作记录
     * $action = array('提交订单', '付款成功', '确认订单', '取消', '等待收货', '完成', '退货');
     */
    public function orderRemarkLog($order_id, $action, $note = '') {
        $action = strtolower($action);
        if ($action = 'cancel') {
            $status_desc = '取消确认';
        } else if ($action == 'confirm') {
            $status_desc = '确认订单';
        } else if ($action == 'pay') {
            $status_desc = '已付款';
        } else if ($action == 'paycancel') {
            $status_desc = '取消付款';
        } else if ($action == 'invalid') {
            $status_desc = '作废订单';
        } else if ($action == 'remove') {
            $status_desc = '移除订单';
        } else if ($action == 'delivery_confirm') {
            $status_desc = '确认收货';
        } else {
            $status_desc = $action;
        }

        $order = M('order')->where(array('order_id' => $order_id))->find();
        $data['order_id'] = $order_id;
        $data['action_user'] = session('admin_id');
        $data['action_note'] = $note;
        $data['order_status'] = $order['order_status'];
        $data['pay_status'] = $order['pay_status'];
        $data['shipping_status'] = $order['shipping_status'];
        $data['log_time'] = time();
        $data['status_desc'] = $status_desc;
        return M('order_remark')->add($data);
    }

    /**
     * 获取订单商品总价格
     */
    public function getGoodsAmount($order_id) {
        $sql = "SELECT SUM(goods_num * goods_price) AS goods_amount FROM __PREFIX__order_goods WHERE order_id = {$order_id}";
        $res = $this->query($sql);
        return $res[0]['goods_amount'];
    }

    /**
     * 得到发货单流水号
     */
    public function get_delivery_sn() {
        /* 选择一个随机的方案 */
        send_http_status('310');
        mt_srand((double) microtime() * 1000000);
        return date('YmdHi') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
    }

    /**
     * 获取当前可操作的按钮
     */
    public function getOrderButton($order) {
        /*
         *  操作按钮汇总 ：付款、设为未付款、确认、取消确认、无效、去发货、确认收货、申请退货
         * 
         */
        $os = $order['order_status']; //订单状态
        $ss = $order['shipping_status']; //发货状态
        $ps = $order['pay_status']; //支付状态
        $btn = array();
        if ($order['pay_code'] == 'cod') {
            if ($os == 0 && $ss == 0) {
                $btn['confirm'] = '确认';
            } elseif ($os == 1 && $ss == 0) {
                $btn['delivery'] = '去发货';
                $btn['cancel'] = '取消确认';
            } elseif ($ss == 1 && $os == 1 && $ps == 0) {
                $btn['pay'] = '付款';
            } elseif ($ps == 1 && $ss == 1 && $os == 1) {
                $btn['payCancel'] = '设为未付款';
            }
        } else {
            if ($ps == 0 && $os == 0) {
                $btn['pay'] = '付款';
            } elseif ($os == 0 && $ps == 1) {
                $btn['payCancel'] = '设为未付款';
                $btn['confirm'] = '确认';
            } elseif ($os == 1 && $ps == 1 && $ss == 0) {
                $btn['cancel'] = '取消确认';
                $btn['delivery'] = '去发货';
            }
        }

        if ($ss == 1 && $os == 1 && $ps == 1) {
            $btn['delivery_confirm'] = '确认收货';
            $btn['refund'] = '申请退货';
        } elseif ($os == 2 || $os == 4) {
            $btn['refund'] = '申请退货';
        } elseif ($os == 3 || $os == 5) {
            $btn['remove'] = '移除';
        }
        if ($os != 5) {
            $btn['invalid'] = '无效';
        }
        return $btn;
    }

    public function orderProcessHandle($order_id, $act) {
        $updata = array();
        switch ($act) {
            case 'pay': //付款
                $order_sn = M('order')->where("order_id = $order_id")->getField("order_sn");
                update_pay_status($order_sn); // 调用确认收货按钮
                return true;
            case 'payCancel': //取消付款
                $updata['pay_status'] = 0;
                $this->orderPayCancel($order_id);
                return true;
            case 'confirm': //确认订单
                $updata['order_status'] = 1;
                $order = M('order')->where("order_id = $order_id")->find();
                if ($order['pay_code'] == 'cod')
                    minus_stock($order_id);
                break;
            case 'cancel': //取消确认
                $updata['order_status'] = 0;
                $order = M('order')->where("order_id = $order_id")->find();
                if ($order['pay_code'] == 'cod') {
                    $orderGoods = M('order_goods')->where("order_id = $order_id")->find();
                    add_stock($orderGoods['order_id'], $orderGoods['goods_id']);
                }
                break;
            case 'invalid': //作废订单
                $updata['order_status'] = 5;
                break;
            case 'remove': //移除订单
                $this->delOrder($order_id);
                break;
            case 'delivery_confirm'://确认收货
                confirm_order($order_id); // 调用确认收货按钮
                return true;
            default:
                return true;
        }
        return M('order')->where("order_id=$order_id")->save($updata); //改变订单状态
    }

    //管理员取消付款
    function orderPayCancel($order_id) {
        //如果这笔订单已经取消付款过了
        $count = M('order')->where("order_id = $order_id and pay_status = 1")->count();   // 看看有没已经处理过这笔订单  支付宝返回不重复处理操作
        if ($count == 0)
            return false;
        // 找出对应的订单
        $order = M('order')->where("order_id = $order_id")->find();
        // 增加对应商品的库存
        $orderGoodsArr = M('OrderGoods')->where("order_id = $order_id")->select();
        foreach ($orderGoodsArr as $key => $val) {
            if (!empty($val['spec_key'])) {// 有选择规格的商品   // 先到规格表里面增加数量 再重新刷新一个 这件商品的总数量
                M('SpecGoodsPrice')->where("goods_id = {$val['goods_id']} and `key` = '{$val['spec_key']}'")->setInc('store_count', $val['goods_num']);
                refresh_stock($val['goods_id']);
            } else {
                M('Goods')->where("goods_id = {$val['goods_id']}")->setInc('store_count', $val['goods_num']); // 增加商品总数量
            }
            M('Goods')->where("goods_id = {$val['goods_id']}")->setDec('sales_sum', $val['goods_num']); // 减少商品销售量
            //更新活动商品购买量
            if ($val['prom_type'] == 1 || $val['prom_type'] == 2) {
                $prom = get_goods_promotion($val['goods_id']);
                if ($prom['is_end'] == 0) {
                    $tb = $val['prom_type'] == 1 ? 'limit_buy' : 'group_buy';
                    M($tb)->where("id=" . $val['prom_id'])->setDec('buy_num', $val['goods_num']);
                    M($tb)->where("id=" . $val['prom_id'])->setDec('order_num');
                }
            }
        }
        // 根据order表查看消费记录 给他会员等级升级 修改他的折扣 和 总金额
        M('order')->where("order_id=$order_id")->save(array('pay_status' => 0));
//        update_user_level($order['user_id']);
        // 记录订单操作日志
        logOrder($order['order_id'], '订单取消付款', '付款取消', $order['user_id']);
        //分销设置
        M('bonus_log')->where("order_id = {$order['order_id']}")->save(array('status' => 0));
    }

    /**
     * 	处理发货单
     * @param array $data  查询数量
     */
    public function deliveryHandle($data) {
        $order = $this->getOrderInfo($data['order_id']);
        $orderGoods = $this->getOrderGoods($data['order_id']);
        $selectgoods = $data['goods'];
        $data['order_sn'] = $order['order_sn'];
        $data['delivery_sn'] = $this->get_delivery_sn();
        $data['zipcode'] = $order['zipcode'];
        $data['user_id'] = $order['user_id'];
        $data['admin_id'] = session('admin_id');
        $data['consignee'] = $order['consignee'];
        $data['mobile'] = $order['mobile'];
        $data['country'] = $order['country'];
        $data['province'] = $order['province'];
        $data['city'] = $order['city'];
        $data['district'] = $order['district'];
        $data['address'] = $order['address'];
        $data['shipping_code'] = $order['shipping_code'];
        $data['shipping_name'] = $order['shipping_name'];
        $data['shipping_price'] = $order['shipping_price'];
        $data['create_time'] = time();
        $did = M('delivery')->add($data);
        $is_delivery = 0;
        foreach ($orderGoods as $k => $v) {
            if ($v['is_send'] == 1) {
                $is_delivery++;
            }
            if ($v['is_send'] == 0 && in_array($v['rec_id'], $selectgoods)) {
                $res['is_send'] = 1;
                $res['delivery_id'] = $did;
                $r = M('order_goods')->where("rec_id=" . $v['rec_id'])->save($res); //改变订单商品发货状态
                $is_delivery++;
            }
        }
        $updata['shipping_time'] = time();
        if ($is_delivery == count($orderGoods)) {
            $updata['shipping_status'] = 1;
        } else {
            $updata['shipping_status'] = 2;
        }
        M('order')->where("order_id=" . $data['order_id'])->save($updata); //改变订单状态
        $s = $this->orderRemarkLog($order['order_id'], 'delivery', $data['note']); //操作日志
        return $s && $r;
    }

    /**
     * 获取地区名字
     * @param int $p
     * @param int $c
     * @param int $d
     * @return string
     */
    public function getAddressName($p = 0, $c = 0, $d = 0) {
        $p = M('region')->where(array('id' => $p))->field('name')->find();
        $c = M('region')->where(array('id' => $c))->field('name')->find();
        $d = M('region')->where(array('id' => $d))->field('name')->find();
        return $p['name'] . ',' . $c['name'] . ',' . $d['name'] . ',';
    }

    /**
     * 删除订单
     */
    function delOrder($order_id) {
        $a = M('order')->where(array('order_id' => $order_id))->delete();
        $b = M('order_goods')->where(array('order_id' => $order_id))->delete();
        return $a && $b;
    }

}
